//
//  MainFirstBannerView.m
//  Rabbit
//
//  Created by 阿帕 on 2018/11/3.
//  Copyright © 2018年 GCY. All rights reserved.
//

#import "MainFirstBannerView.h"
#import <SDCycleScrollView.h>
#import <JhtMarquee/JhtVerticalMarquee.h>

@interface MainFirstBannerView ()<SDCycleScrollViewDelegate, UIGestureRecognizerDelegate>

@property (strong , nonatomic)SDCycleScrollView *bannerScrollView;

@property (strong , nonatomic)UIImageView *advertScrollView;

@property (strong , nonatomic)JhtVerticalMarquee *verticalMarquee;

@property (strong, nonatomic)UILabel *badgeLabel;

@end

@implementation MainFirstBannerView

#pragma mark - Intial
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    return self;
}

- (void)dealloc {
    [KNotificationCenter removeObserver:self name:LOGIN_SUCCESS object:nil];
}
- (void)setUpUI {
    
    [KNotificationCenter addObserver:self selector:@selector(refreshHomePageData:) name:LOGIN_SUCCESS object:nil];
    
    self.backgroundColor = [UIColor cy_colorWithHexString:@"#F9F9F9"];
    
    //    UIView *bannerBgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW-20, 145)];
    //    bannerBgView.backgroundColor = [UIColor whiteColor];
    //    [self addSubview:bannerBgView];
    
    _bannerScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(10, 0, ScreenW-20, 135) delegate:self placeholderImage:image(@"gcy_img_banner_plaholder")];
    UIImageView *backgroundImageView = [_bannerScrollView valueForKeyPath:@"_backgroundImageView"];
    backgroundImageView.contentMode = UIViewContentModeScaleToFill;
    _bannerScrollView.bannerImageViewContentMode = UIViewContentModeScaleToFill;
    _bannerScrollView.autoScrollTimeInterval = 3.0;
    _bannerScrollView.showPageControl = NO;
    _bannerScrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bannerScrollView];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 145, ScreenW, 10)];
    lineView.backgroundColor = BackgroundColor;
    [self addSubview:lineView];
    
    _advertScrollView = [[UIImageView alloc] initWithFrame:CGRectMake(0, lineView.cy_bottom, ScreenW, 50)];
    _advertScrollView.backgroundColor = [UIColor whiteColor];
    _advertScrollView.userInteractionEnabled = YES;
    //    _advertScrollView.image = image(@"gcy_img_main_advert_bg");
    //    [DCSpeedy dc_chageControlCircularWith:_advertScrollView AndSetCornerRadius:3 SetBorderWidth:0 SetBorderColor:nil canMasksToBounds:YES];
    [self addSubview:_advertScrollView];
    
    //icon
    UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 30)];
    icon.contentMode = UIViewContentModeScaleAspectFit;
    icon.image = image(@"gcy_img_main_advert");
    [_advertScrollView addSubview:icon];
    
    //滚动视图
    self.verticalMarquee.frame = CGRectMake(icon.cy_right + 10, 0, _advertScrollView.cy_width - icon.cy_width - 15 , _advertScrollView.cy_height);
    
    [self.verticalMarquee scrollWithCallbackBlock:^(JhtVerticalMarquee *view, NSInteger currentIndex) {
        //        NSLog(@"滚动到第 %ld 条数据", (long)currentIndex);cy_labelAttributedString
    }];
    
    //4个button
    NSArray *titles = @[@"运单中心", @"在线油气", @"运费统计", @"通知消息"];
    NSArray *btnImages = @[@"gcy_icon_main_yundan", @"gcy_icon_main_qiyou",@"gcy_icon_main_yunfei",@"gcy_icon_main_notice"];
    for (int i=0; i<4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 1000 + i;
        [button setBackgroundColor:[UIColor whiteColor]];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        button.titleLabel.font = font(14);
        [button setTitleColor:Dark_TextColor forState:UIControlStateNormal];
        [button setImage:image(btnImages[i]) forState:UIControlStateNormal];
        button.frame = CGRectMake(ScreenW/4*i, _advertScrollView.cy_bottom + 10, ScreenW/4, 110);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 3) {
            CGFloat badgeWidth = 20;
            CGFloat badgeX = button.frame.size.width - (button.frame.size.width - button.imageView.size.width) / 2 - badgeWidth;
            CGFloat badgeY = button.imageView.origin.y - badgeWidth / 2;
            self.badgeLabel = [[UILabel alloc] initWithFrame:CGRectMake(badgeX, badgeY, badgeWidth, badgeWidth)];
            self.badgeLabel.hidden = YES;
            self.badgeLabel.textColor = [UIColor whiteColor];
            self.badgeLabel.font = [UIFont systemFontOfSize:10];
            self.badgeLabel.textAlignment = NSTextAlignmentCenter;
            self.badgeLabel.layer.backgroundColor = [UIColor redColor].CGColor;
            self.badgeLabel.layer.cornerRadius = badgeWidth / 2;
            [button addSubview:self.badgeLabel];
        }
        //        if (i == 1) {
        //            if ([[UserDefaultUtil valueForKey:HOME_OILPAYTYPE] integerValue] == 0) {
        //                [button setTitle:titles[i] forState:UIControlStateNormal];
        //                [button setImage:image(btnImages[i]) forState:UIControlStateNormal];
        //            } else {
        //                [button setTitle:@"在线汽油" forState:UIControlStateNormal];
        //                [button setImage:image(@"gcy_icon_main_qiyou") forState:UIControlStateNormal];
        //            }
        //        }
        [self addSubview:button];
        
        [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:10];
        
    }
}

- (void)setUnreadNoticeTotal:(NSInteger)unreadNoticeTotal {
    _unreadNoticeTotal = unreadNoticeTotal;
    
    self.badgeLabel.hidden = unreadNoticeTotal <= 0;
    if (self.unreadNoticeTotal >= 100) {
        self.badgeLabel.text = @"99+";
    } else {
        self.badgeLabel.text = [NSString stringWithFormat:@"%ld", unreadNoticeTotal];
    }
}

- (void)setImageGroupArray:(NSArray *)imageGroupArray
{
    _imageGroupArray = imageGroupArray;
    if (imageGroupArray.count == 0) {
        _bannerScrollView.localizationImageNamesGroup = @[@"gcy_m_banner1", @"gcy_m_banner2"];
    } else {
        _bannerScrollView.imageURLStringsGroup = imageGroupArray;
    }
}

- (void)setAdvertArray:(NSArray *)advertArray {
    
    _advertArray = advertArray;
    //    NSMutableArray *muArr = [NSMutableArray array];
    //
    //    for (NSString *string in _advertArray) {
    //        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
    //        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(17, attrStr.length-18)];
    //        [muArr addObject:attrStr];
    //    }
    //    self.verticalMarquee.sourceArray = muArr;
    if (TOKEN_ISEMPTY) {
        
//        self.verticalMarquee.sourceArray = @[@"暂无"];
        self.verticalMarquee.sourceArray = @[@""];
        
    } else {
        
        if (advertArray.count != 0) {
            
            NSMutableArray *arr = [[NSMutableArray alloc] init];
            
            [advertArray enumerateObjectsUsingBlock:^(FreightIndexModel  *model, NSUInteger idx, BOOL * _Nonnull stop) {
                NSString *title = CJStringWithFormat(@"%@→%@  %.2f元/吨", model.shipperAddress, model.consigneeAddress, model.price);
                NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:title];
                NSRange blueRange = NSMakeRange([[attrStr string] rangeOfString:@"→"].location, [[attrStr string] rangeOfString:@"→"].length);
                NSRange redRange = NSMakeRange([[attrStr string] rangeOfString:CJStringWithFormat(@"%.2f元/吨", model.price)].location, [[attrStr string] rangeOfString:CJStringWithFormat(@"%.2f元/吨", model.price)].length);
                [attrStr addAttribute:NSForegroundColorAttributeName value:Theme_ColorValue range:blueRange];
                [attrStr addAttribute:NSForegroundColorAttributeName value:Orange_Color range:redRange];
                
                [arr addObject:attrStr];
            }];
            
            self.verticalMarquee.sourceArray = arr;
            
        } else {
            self.verticalMarquee.sourceArray = @[@"暂无"];
        }
    }
    
    // 开始滚动
    [self.verticalMarquee marqueeOfSettingWithState:MarqueeStart_V];
    
}

#pragma mark - 点击图片Bannar跳转
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(firstBannerIndexClick:)]) {
        [self.delegate firstBannerIndexClick:index];
    }
}

- (void)buttonClick:(UIButton *)button {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(buttonIndexClick:)]) {
        [self.delegate buttonIndexClick:button.tag - 1000];
    }
}


/** 纵向 跑马灯 */
- (JhtVerticalMarquee *)verticalMarquee {
    if (!_verticalMarquee) {
        _verticalMarquee = [[JhtVerticalMarquee alloc]  initWithFrame:CGRectMake(0, 0, ScreenW-95, _advertScrollView.cy_height)];
        _verticalMarquee.tag = 101;
        _verticalMarquee.verticalNumberOfLines = 2;
        _verticalMarquee.backgroundColor = [UIColor clearColor];
        _verticalMarquee.verticalTextColor = Dark_TextColor;
        _verticalMarquee.verticalTextFont = font(13);
        _verticalMarquee.userInteractionEnabled = YES;
        [_advertScrollView addSubview:self.verticalMarquee];
        // 添加点击手势
        UITapGestureRecognizer *vtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(marqueeTapGes:)];
        [_verticalMarquee addGestureRecognizer:vtap];
    }
    
    return _verticalMarquee;
}

/** 点击 滚动跑马灯 触发方法 */
- (void)marqueeTapGes:(UITapGestureRecognizer *)ges {
    if (ges.view.tag == 100) {
        NSLog(@"点击__水平__滚动的跑马灯啦！！！");
        
    } else if (ges.view.tag == 101) {
        NSLog(@"点击__纵向__滚动的跑马灯_第 %ld 条数据啦！！！", (long)self.verticalMarquee.currentIndex);
        if (self.delegate && [self.delegate respondsToSelector:@selector(verticalMarqueeIndexClick:)]) {
            [self.delegate verticalMarqueeIndexClick:self.verticalMarquee.currentIndex];
        }
    }
    
    //    [self.verticalMarquee marqueeOfSettingWithState:MarqueePause_V];
}

- (void)refreshHomePageData:(NSNotification *)notification {
    
    //    UIButton *btn = [self viewWithTag:1001];
    //    //0是个人信息 1在线汽油
    //    NSInteger oilPayType = [[UserDefaultUtil valueForKey:HOME_OILPAYTYPE] integerValue];
    //    if (oilPayType == 1) {
    //        [btn setTitle:@"在线汽油" forState:UIControlStateNormal];
    //        [btn setImage:image(@"gcy_icon_main_qiyou") forState:UIControlStateNormal];
    //    } else {
    //        [btn setTitle:@"账户信息" forState:UIControlStateNormal];
    //        [btn setImage:image(@"gcy_icon_main_zhanghu") forState:UIControlStateNormal];
    //    }
}
@end
